class SearchController < ApplicationController
  layout "normal"

  def index 
   user = session[:user]
   @users = []
   if user.is_administrator?
     @projects=user.company.projects
     @users=user.company.users
   else
     @projects=user.projects
      @projects.each do |p|
        p.users.each do |u|
         if not @users.member?(u)
           @users += u.to_a 
		 end
		end
	  end
   end

   @s_projects={}
   @s_users={}
  end

  def search_result
   user = session[:user]
   @s_projects= session[:s_project]
   @s_users= session[:s_user]

   @users = []
   if user.is_administrator?
     @projects=user.company.projects
     @users=user.company.users 
   else
     @projects=user.projects
      @projects.each do |p|
        p.users.each do |u|
         if not @users.member?(u)
           @users += u.to_a 
		 end
		end
	  end
   end


    if request.post?
     #get selected projects
     @s_projects = params[:s_projects]
	 if @s_projects
	    p_array = @s_projects.collect {|p| p + ","}
	    p_array.last.sub!(',','') 	
	 end
	 session[:s_project] = @projects= Project.find(:all,:conditions=>["id IN(#{p_array.to_s})"])

     #get selected users
     @s_users = params[:s_users]
	 if @s_users
	    u_array = @s_users.collect {|u| u + ","}
	    u_array.last.sub!(',','') 	
	 end
	 session[:s_user] = @users = User.find(:all,:conditions=>["id IN(#{u_array.to_s})"])

	 start_at = params[:time_log][:start_at]
	 @confirmed_time = params[:time_log][:confirmed_time]
	 @work_time      = params[:time_log][:work_time]
	 @order_by       = params[:time_log][:order_by]


	 @start_at = start_at[:year].to_s + "-" + start_at[:month].to_s + "-" + start_at[:day].to_s + " " + start_at[:hour].to_s + ":" + start_at[:minute].to_s + ":" + "00"
	 end_at   = params[:time_log][:end_at]
	 @end_at   = end_at[:year].to_s + "-" + end_at[:month].to_s + "-" + end_at[:day].to_s + " " + end_at[:hour].to_s + ":" + end_at[:minute].to_s + ":" + "00"

      cfm_sql =""
	  if @confirmed_time == "confirmed time"
	     cfm_sql = " AND confirmed = 1 "
	  elsif @confirmed_time == "unconfirmed time"
	     cfm_sql = " AND confirmed = 0 "
	  else
	     cfm_sql = " "
	  end
	  work_sql = ""
	  if @work_time == "work time"
	     work_sql = " AND after_work_time = 0 "
	  elsif @work_time == "after work time"
	     work_sql = " AND after_work_time = 1 "
	  else
	     work_sql = ""
	  end

	  order_by =""
	  if @order_by == "Project"
	     order_by = " project_id DESC,id DESC"
	  elsif @order_by == "User"
	     order_by = " User_id DESC,id DESC"
	  end
	  
      session['condition'] = "user_id IN(#{u_array.to_s}) AND project_id IN(#{p_array.to_s}) AND start_at > '#{@start_at}' AND end_at < '#{@end_at}'" + cfm_sql + work_sql 
	  session['order_by'] = order_by
	end

     @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>[session['condition']],:order => session['order_by']
	 if @page == 1 or request.post?
      render :update do |page|
	   page.replace_html 'result',:partial=>'time_log'
	  end
	 end
  end

  def confirm
		@user  = session[:user]
		t = TimeLog.find(params[:id])
		t.confirmed = 1
		t.save
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>[session['condition']],:order => session['order_by']
	   render :partial=>'time_log'
  end
  def unconfirm
		@user  = session[:user]
		t = TimeLog.find(params[:id])
		t.confirmed = 0
		t.save
       @time_log_pages, @time_logs = paginate :time_logs, :per_page => 10,:conditions=>[session['condition']],:order => session['order_by']
	   render :partial=>'time_log'
  end
end
